package com.cade.agriculturemall.view.classify.goodsdetail;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatDialog;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.cade.agriculturemall.R;
import com.cade.agriculturemall.widget.imgView.PhotoView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hshunxin on 2018/3/6.
 */

public class ImageViewDialog extends AppCompatDialog {
    private Context mContext;
    private ViewPager mPager;
    private List<String> imgList = new ArrayList<>();
    private int currentPos;

    public ImageViewDialog(Context context, List<String> imgList, int position) {
        super(context);
        mContext = context;
        this.imgList = imgList;
        currentPos = position;
        init();
    }

    private void init() {
        setContentView(R.layout.layout_img_view);
        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setPageMargin((int) (mContext.getResources().getDisplayMetrics().density * 15));

        mPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return imgList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                PhotoView view = new PhotoView(mContext);
                view.enable();
                view.setScaleType(ImageView.ScaleType.FIT_CENTER);
                Glide.with(mContext).load(imgList.get(position)).asBitmap().into(view);
//                view.setImageResource();
                container.addView(view);
                return view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
        });
        mPager.setCurrentItem(currentPos);//使用ViewPager的setCurrentItem (int item) 方法设置其初始显示的页面，不是在其数据适配器中，而是在完成数据适配后设置。
    }
}
